import { graphqlKoa, graphiqlKoa } from 'graphql-server-koa'
import { fetchInfo, saveInfo, updateInfo } from '../controllers/info'
import { sendEmail } from '../controllers/sendEmail'
import schema from '../graphql/schema'

const router = require('koa-router')()

// 重定向至dist目录
const redirect_test = ctx => {
  ctx.response.redirect('/dist')
}

router
  .post('/fetchInfo', fetchInfo)
  .post('/saveInfo', saveInfo)
  .post('/updateInfo', updateInfo)
  .get('/sendEmail', sendEmail)
  .get('/', redirect_test)

router
  .post('/graphql', async (ctx, next) => {
    await graphqlKoa({ schema: schema })(ctx, next)
  })
  .get('/graphql', async (ctx, next) => {
    await graphqlKoa({ schema: schema })(ctx, next)
  })
  .get('/graphiql', async (ctx, next) => {
    await graphiqlKoa({ endpointURL: '/graphql' })(ctx, next)
  })

module.exports = router
